package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping(path = "/permission")
@CrossOrigin
public class PermissionController {

    @Reference
    private PermissionService permissionService;


    //查询所有的权限信息
    @GetMapping("/all")
    public Result findAllPermission() {
        try {
            List<Permission> permission = permissionService.findAllPermission();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }


    //查询权限管理
    @GetMapping
    public Result findAll(QueryPageBean queryPageBean){
        try {
            PageResult pageResult = permissionService.findAll(queryPageBean);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //新增权限管理
    @PostMapping
    public Result add(@RequestBody Permission permission){
        return permissionService.add(permission.getName(), permission.getKeyword(), permission.getDescription());
    }

    //修改权限管理
    @PutMapping
    public Result update(@RequestBody Permission permission){
        return permissionService.update(permission.getName(), permission.getKeyword(), permission.getDescription(),permission.getId());
    }

    //弹出编辑框
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") Integer id){
        return permissionService.findById(id);
    }

    //删除权限管理
    @DeleteMapping(path = "/{id}")
    public Result delete(@PathVariable("id") Integer id){
        return permissionService.delete(id);
    }





    //yh角色管理使用根据菜单id查询权限详情
    @GetMapping("/menu/association")
    public Result findByMenuIds(Integer[] menuIds){
        //1.根据菜单id查询权限
        List<Permission> list = permissionService.findByMenuId(menuIds);

        return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,list);
    }


}
